<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        if (empty($_SESSION['user']['name'])) {
            $this->redirect('Index/log');
            exit;
        }
    	$data = M('container')->order('id desc')->select();
    	$link_list = M('link')->select();
    	$this->assign('link_list',$link_list);
        $this->assign('list',$data);
        $this->assign('del',"<a href='#'>查看更多</a>");
        if (empty($_SESSION['user']['name'])) {
            $this->assign('name',"<a href=".U('Index/log').">请登录</a>");
        } else {
            $this->assign('name',$_SESSION['user']['name']);
        }
        
        $this->display();
    }
    public function log()
    {	
        $this->display();
    }
    public function dolog()
    {
        $name = $_POST['name'];
        $pass = $_POST['pass'];
        $model = M('admin');
        $map['name'] = $name;
        $map['pass'] = $pass;
        $myData = $model->where($map)->select();
        // print_r($myData[0]);exit;
        // $this->assign('cover',$myData[0]);
        if (!empty($myData)) {
            //登陆成功 用户信息存入session
            $_SESSION['user'] = $myData[0];
            // print_r($_SESSION['user']);
            $this->success('登陆成功!', U('index'));
        } else {
            $model2 = M('user');
            $myData = $model2->where($map)->select();
            if (!empty($myData)){
                //登陆成功 用户信息存入session
                $_SESSION['user'] = $myData[0];
                
                $this->success('登陆成功!', U('index'));
            }else{
                $this->error('登录失败!!!');  
            }
            
        }

    }
    public function logOut()
    {
        unset($_SESSION['user']);
        $this->success('成功退出当前账号!',U('index'));
    }
    public function reg()
    {	
        $this->display();
    }
    public function doreg()
    {
        if (empty($_POST)) {
            $this->error('请填写数据!', U('Index/reg'));
            exit;
        }
        
        //生成数据对象
        M('user')->create();

        //添加操作 ,返回的字自增ID
        if (M('user')->add() > 0) {
            $this->success('请登录',U('log'));
        } else {
            $this->error('添加失败');
        }
    }
}